<?php
namespace app\admin\controller;
use think\Db;

class Main extends SessionCheck
{
    public function index()
    {
        $where = [];
        $aid = request()->request('aid');
        if(!empty($aid)){
            $where['aid'] = $aid;
        }
        $beginToday = strtotime(date('Y-m-d'));
        $endToday = $beginToday+86400;
        $data['sum_order_count'] = Db::table("order")->whereIn('aid',session('aid'))->where($where)->count();
        $data['today_order_count'] = Db::table("order")->whereIn('aid',session('aid'))->where($where)->where(['issue_time'=>['between',$beginToday.','.$endToday]])->count();
        $data['express_total'] = Db::table("user")->whereIn('aid',session('aid'))->where($where)->where('isExpress',1)->count();
        $system_money = Db::table("order")->whereIn('aid',session('aid'))->where($where)->where(['issue_time'=>['between',$beginToday.','.$endToday]])->select();
        $money_total = 0;
        foreach ($system_money as $k => $v){
            $money_total += $v['system_money'];
        }
        $data['money_total'] = $money_total;
        $today_begin_time = $beginToday;
        $today_end_time = $endToday;
        $date_data_array = [];
        $date_string = '';
        for($i=7;$i>0;$i--){
            if($i != 7){
                $date_string .= date('Y-m-d', strtotime('-'.$i.' days')).',';
            }
            $date_data_array[] = Db::table("order")->whereIn('aid',session('aid'))->where($where)->where(['issue_time'=>['between',$today_begin_time.','.$today_end_time]])->count();
            $today_begin_time -= 86400;
            $today_end_time -= 86400;
        }
        $date_array = explode(',',$date_string);
        $date_money_total_string = '';
        foreach ($date_array as $k => $v){
            if($v){
                $today_begin_time = strtotime($v);
                $today_end_time = $today_begin_time+86400;
                $system_money = Db::table("order")->whereIn('aid',session('aid'))->where($where)->where(['issue_time'=>['between',$today_begin_time.','.$today_end_time]])->select();
                $money_total = 0;
                foreach ($system_money as $k2 => $v2){
                    $money_total += $v2['system_money'];
                }
                $date_money_total_string .= '{value:"'.$money_total.'",name:"'.$v.'"},';
            }

        }
        $date_money_total_string .= '{value:"'.$data['money_total'].'",name:"'.date('Y-m-d').'"},';
        $date_money_total_string = substr($date_money_total_string,0,strlen($date_money_total_string)-1);
        $date_string .= date('m-d');
        $date_data = implode(',',array_reverse($date_data_array));
       // dump(session('aid'));exit;
        return view('',['data'=>$data,'date_array'=>$date_string,'date_data'=>$date_data,'date_money_total_string'=>$date_money_total_string,'menu_list'=>$this->menu_list]);
    }

    public function auth()
    {
        $tab = input("get.tab");
        if(request()->isPost()){
            $data = input("post.");
            Db::table("webconfig")->where(['id'=>1])->update($data);
        }
        $data = Db::table("webconfig")->where(['id'=>1])->find();
        return view('',['data' => $data,'tab' => $tab]);
    }

    public function setting()
    {
        $tab = input("get.tab");
        if(request()->isPost()){
            $data = request()->post();
            $edit = $data['edit'];
            unset($data['edit']);
            if($edit == 'version'){
                $type = $data['type'];
                if(!is_dir(ROOT_PATH.'/version/')){
                    mkdir(ROOT_PATH.'/version/');
                }
                $file = file_get_contents($_FILES['apk']['tmp_name']);
                $name = $type.str_replace('.','_',$data['version']).'.apk';
                file_put_contents(ROOT_PATH.'/version/'.$name,$file);
                $data['time'] = time();
                $data['url'] = 'http://'.$_SERVER['HTTP_HOST'].'/version/'.$name;
                $data['size'] = filesize($_FILES['apk']['tmp_name']);
                Db::table('update')->insert($data);
            }elseif ($edit == 'download'){
                Db::table('download')->where('id',1)->update($data);
            }
        }
        $data = Db::table('download')->where('id',1)->find();
        return view('',['data' => $data,'tab' => $tab,'menu_list'=>$this->menu_list]);
    }

}